/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	 Copyright (C) 2011
 *   @author prebz
 */

package com.wirelesstransfer.activity.receive;

import com.wirelesstransfer.R;
import com.wirelesstransfer.activity.dialogs.Question;
import com.wirelesstransfer.client.ApacheClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class CodeAuthentication extends Activity implements OnClickListener{

	private EditText codeEditText;
	private Authenticate authenticate;

	private int code;
	public static int ILLEGALLENGTH = -2;

	public static final int DIALOG_ILLEGAL_LENGTH = 0;
	public static final int DIALOG_NON_DIGIT = 1;
	public static final int DIALOG_INCORRECT_CODE = 2;

	private Resources res;	

	private ApacheClient client;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.code_authentication);

		res = getResources();
		
		client = ApacheClient.getInstance();
		
		// Clicklistener for button
		View getFilesButton = findViewById(R.id.getfiles_button);
		getFilesButton.setOnClickListener(this);

		codeEditText = (EditText) findViewById(R.id.code_edit);
		codeEditText.setOnClickListener(this);

		authenticate = new Authenticate();

		code = -1;

	}

	public void onClick(View v) {
		switch(v.getId()){
		case R.id.getfiles_button:
			code = authenticate.getValueCode(codeEditText.getText().toString());
			if (code == ILLEGALLENGTH)
				showDialog(DIALOG_ILLEGAL_LENGTH);
			else {
				if(client.authenticate(authenticate.getValueCode(codeEditText.getText().toString()))) {
					Intent codeAuthentication = new Intent(this, FileList.class);
					startActivity(codeAuthentication);	
				} else {
					showDialog(DIALOG_INCORRECT_CODE);
				}
			}
			break;

		}
	}

	@Override
	protected Dialog onCreateDialog (int id){
		Dialog dialog = null;	
		switch (id) {
		case DIALOG_ILLEGAL_LENGTH:
			dialog = buildOKDialog(res.getString(R.string.codeauthentication_ILLEGAL_LENGTH));
			break;
		case DIALOG_NON_DIGIT:
			dialog = buildOKDialog(res.getString(R.string.codeauthentication_NON_DIGIT));
			break;	
		case DIALOG_INCORRECT_CODE:
			dialog = buildOKDialog(res.getString(R.string.codeauthentication_INCORRECT_CODE));
			break;
		default:
			dialog = null;
			break;
		}
		return dialog;

	}

	/**
	 * Creates text in a dialog with Ok key
	 * @param action
	 * @return
	 */
	private AlertDialog buildOKDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		.setCancelable(false)

		.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		return alert;
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.questionmenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.question_menu:
			Intent question = new Intent(this, Question.class);
			question.putExtra(res.getString(R.string.question_TITLE),res.getString(R.string.question_code_title_txt));
			question.putExtra(res.getString(R.string.question_TEXT),res.getString(R.string.question_code_txt));
			startActivity(question);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
